package org.zfes.snowier.zbss.xzy.biz.config.service.impl;

import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;
import org.zfes.snowier.zbss.xzy.biz.config.dao.MoConfigItemMapper;
import org.zfes.snowier.zbss.xzy.biz.config.model.MoConfigItem;
import org.zfes.snowier.zbss.xzy.biz.config.service.IMoConfigItemService;

import java.util.stream.Stream;
@Service
public class MoConfigItemServiceImpl implements IMoConfigItemService{
 	@Autowired
	private MoConfigItemMapper moConfigItemMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createMoConfigItem(MoConfigItem moConfigItem) {
		ZBeanUtil.setCreateInfo(moConfigItem);
		ZBeanUtil.validateBean(moConfigItem);
		
		Object id1=moConfigItemMapper.selectIdByCode(moConfigItem.getCode());
		if(id1!=null){
			ZAlert.Error("配置编码已经存在");
		}
		
		Object id2=moConfigItemMapper.selectIdByName(moConfigItem.getName());
		if(id2!=null){
			ZAlert.Error("配置名称已经存在");
		}
		moConfigItemMapper.insertSelective(moConfigItem);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<MoConfigItem> loadMoConfigItemById(Long id) {
		if(id!=null){
			return Optional.ofNullable(moConfigItemMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateMoConfigItem(MoConfigItem moConfigItem) {
		ZBeanUtil.setUpdatedInfo(moConfigItem);
		ZBeanUtil.validateBean(moConfigItem);
		Object id1=moConfigItemMapper.selectIdByCode(moConfigItem.getCode());
		if(id1!=null&&!id1.equals(moConfigItem.getId())){
			ZAlert.Error("配置编码已经存在");
		}
		Object id2=moConfigItemMapper.selectIdByName(moConfigItem.getName());
		if(id2!=null&&!id2.equals(moConfigItem.getId())){
			ZAlert.Error("配置名称已经存在");
		}
		moConfigItemMapper.updateByPrimaryKeySelective(moConfigItem);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteMoConfigItem(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		moConfigItemMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setMoConfigItemStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		moConfigItemMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadMoConfigItemDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		pm.getObj("name").ifPresent(v->pm.updateParam("name", "%"+v+"%"));
		DataSet ds=DataSet.newDS2(moConfigItemMapper.selectListCount(pm), moConfigItemMapper.selectMapListPage(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<MoConfigItem> loadByCode(java.lang.String code){
		if(code!=null){
			return Optional.ofNullable(moConfigItemMapper.selectByCode(code));
		}
		return Optional.empty();
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<MoConfigItem> loadByName(java.lang.String name){
		if(name!=null){
			return Optional.ofNullable(moConfigItemMapper.selectByName(name));
		}
		return Optional.empty();
	}
}